package com.aspire.core.match.ua.object;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

public class JSONToObjectUtils {

	public static Device toDevice( JSONObject obj )
	{
		Device device = new Device();
		
		device.setBrand_id(obj.getInt("BRAND_ID"));
		device.setDevice_desc(getString(obj,"DEVICE_DESC"));
		device.setDevice_first_alpha(getString(obj,"DEVICE_FIRST_ALPHA"));
		device.setDevice_id(obj.getInt("DEVICE_ID"));
		device.setDevice_name(getString(obj,"DEVICE_NAME"));
		device.setDevice_os_ua(null);
		device.setDevice_ua(null);
		device.setEnglish_device_desc(getString(obj,"ENGLISH_DEVICE_DESC"));
		device.setEnglish_device_name(getString(obj,"ENGLISH_DEVICE_NAME"));
		device.setOs_detail(getString(obj,"OS_DETAIL"));
		device.setOs_id(obj.getInt("OS_ID"));
		device.setPreviewPicwww(getString(obj,"PREVIEW_PIC_WWW"));
		device.setScreensize(getString(obj,"SCREENSIZE"));
		device.setSeries_id(obj.getInt("SERIES_ID"));
		
		if( StringUtils.equalsIgnoreCase(getString(obj,"IS_TOUCH_SCREEN"), "no") )
		{
			device.setTouchScreen(false);
		}
		else
		{
			device.setTouchScreen(true);
		}
		
		return device;
	}
	
	
	
	public static DeviceOS toDeviceOS( JSONObject obj )
	{
		DeviceOS deviceOs = new DeviceOS();
		
		deviceOs.setOs_id(obj.getInt("OS_ID"));
		deviceOs.setOsname(getString(obj,"OSNAME"));
		deviceOs.setOsvendor(getString(obj,"OSVENDOR"));
		
		return deviceOs;
	}
	
	
	
	public static DeviceBrand toDeviceBrand( JSONObject obj )
	{
		DeviceBrand deviceBrand = new DeviceBrand();
		
		deviceBrand.setBrand_desc(getString(obj,"BRAND_DESC"));
		deviceBrand.setBrand_id(obj.getInt("BRAND_ID"));
		deviceBrand.setBrand_name(getString(obj,"BRAND_NAME"));
		deviceBrand.setEnglish_brand_name(getString(obj,"ENGLISH_BRAND_NAME"));
		
		return deviceBrand;
	}
	
	
	
	public static DeviceKeyword toDeviceKeyword( JSONObject obj )
	{
		DeviceKeyword deviceKeyword = new DeviceKeyword();
		
		deviceKeyword.setDevice_id(obj.getInt("DEVICE_ID"));
		deviceKeyword.setKeyword1(getString(obj,"KEYWORD1"));
		deviceKeyword.setKeyword2(getString(obj,"KEYWORD2"));
		deviceKeyword.setKeyword3(getString(obj,"KEYWORD3"));
		
		return deviceKeyword;
	}
	
	
	public static Series toSeries( JSONObject obj )
	{
		Series series = new Series();
		series.setEng_series_desc(getString(obj,"ENG_SERIES_DESC"));
		series.setSeries_id(obj.getInt("SERIES_ID"));
		series.setSeries_os_id(obj.getInt("SERIES_OS_ID"));		
		return series;
	}
	
	public static FilterDevice toFilterDevice( JSONObject obj )
	{
		FilterDevice filterDevice = new FilterDevice();
		filterDevice.setFulldeviceid(obj.getInt("FULLDEVICEID"));	
		return filterDevice;
	}
	
	public static DeviceGWUA toDeviceGWUA( JSONObject obj )
	{
		DeviceGWUA deviceGWUA = new DeviceGWUA();
		deviceGWUA.setDevice_gw_ua(getString(obj,"DEVICE_GW_UA"));
		deviceGWUA.setDevice_id(obj.getInt("DEVICE_ID"));
		return deviceGWUA;
	}
	
	
	public static DeviceUA toDeviceUA( JSONObject obj )
	{
		DeviceUA deviceUA = new DeviceUA();
		
		deviceUA.setDevice_id(obj.getInt("DEVICE_ID"));
		deviceUA.setDevice_os_ua(getString(obj,"DEVICE_OS_UA"));
		deviceUA.setDevice_ua(getString(obj,"DEVICE_UA"));
		deviceUA.setDevice_ua_header(getString(obj,"DEVICE_UA_HEADER"));
		
		return deviceUA;
	}
	
	
	
	
	public static String getString( JSONObject obj, String key )
	{
		String retStr = StringUtils.trimToEmpty(obj.getString(key));
		if( StringUtils.equalsIgnoreCase("null", retStr) )
		{
			retStr = "";
		}
		
		return retStr;
	}
}
